package com.LeeCode;

/**
 * 水果成篮 II
 */

public class Code3477 {
    public static void main(String[] args) {
        int[] fruits = {4, 2, 5};
        int[] baskets = {3, 5, 4};
        System.out.println(new Code3477().numOfUnplacedFruits(fruits, baskets));
    }

    public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
        int unplacedCount = 0;
        // 按顺序处理每种水果
        for (int fruitQuantity : fruits) {
            boolean placed = false;
            // 寻找最左侧的可用且容量足够的篮子
            for (int j = 0; j < baskets.length; j++) {
                if (baskets[j] >= fruitQuantity) {
                    baskets[j] = 0;
                    placed = true;
                    break;
                }
            }
            if (!placed) {
                unplacedCount++;
            }
        }
        return unplacedCount;
    }
}
